package org.hot100_1;

import org.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class leetcode41 {


    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        printNode(root,res);
        return res;
    }

    private void printNode(TreeNode root,List<List<Integer>> res){
        if (root==null) {
            return;
        }
        Deque<TreeNode> deque = new LinkedList<>();
        deque.add(root);

        while (!deque.isEmpty()){
            List<Integer> item = new ArrayList<>();
            int size = deque.size();
            while (size>0){
                TreeNode node = deque.poll();
                item.add(node.val);

                if (node.left!=null) deque.add(node.left);
                if (node.right!=null) deque.add(node.right);
                size--;
            }
            res.add(item);
        }
    }
}
